<?php if(!empty($pagos)): ?>

<div class="row-fluid">
  <div class="span12">
     <h3 class="heading">Detalle de pagos</h3>
     <h3 class="heading">Alumno: <?php echo $nombreAlumno; ?></h3>
     <?php $idAlumno = $alumno['Alumno']['id']; ?>
     <?php echo $this->Form->create('Alumnos', array('action'=>'cancelarpago')); ?>
<table class="table table-striped table-bordered dTableR" id="dt_b">
    <thead>
        <tr>
            <!--<th>Item</th>-->
            <th>Plan</th>
            <th>Pago</th>
            <th>Monto</th>
            <th>Pagado</th>                      
        </tr>
    </thead>
    <tbody>    
    <?php 
     
    $i=0;
    ?>
        <?php foreach($pagos as $pago): ?>
           <?php 
           $idPago = $pago['Pago']['id'] 
           ?>
           <tr>
              <!--<td><?php //echo $idPago ?></td>-->
              <td><?php echo $pago['Planespago']['nombre'] ?></td>
              <td><?php echo $pago['Pago']['nombre'] ?></td>
              <td><?php echo $pago['Pago']['monto'] ?></td>
              <td>
              <?php if(!$pago['Pago']['pagado']): ?>
                  <?php /*echo $this->Html->link('pagar', 
                      array('action'=>'pagarpago', $idAlumno, $idPago, $pago['Pago']['monto']),
                      array('class'=>'btn btn-warning')
                  );*/?>
                  <?php
                  echo $this->Form->hidden("AlumnosPago.$i.alumno_id", array('value'=>$idAlumno));   
                  echo $this->Form->hidden("AlumnosPago.$i.pago_id", array('value'=>$idPago));                                      
                    echo $this->Form->checkbox("AlumnosPago.$i.monto", array(
                        'value' => $pago['Pago']['monto'],
                        'hiddenField' => 'N',
                    )); 
                  $i++;
                  ?> Pagar
              <?php else: ?>
                <a class="btn btn-gebo" href="#">
                <i class="splashy-check"></i>
                Si
                </a>
              <?php endif; ?>
              </td>
           </tr>
        <?php endforeach; ?>
        <tr>
            <td colspan="4">
                Total : <?php echo $this->Form->text('montoTotal', array('id'=>'montoTotal', 'disabled'=>'disabled', 'class'=>'span2')); ?>                
                <?php echo $this->Form->hidden('total', array('id'=>'montoTotalH')); ?>                
            </td>
        </tr>
        <tr>
            <td colspan="2">
                Monto : <?php echo $this->Form->text('cambio', array('id'=>'pagado', 'placeholder'=>0, 'class'=>'span4')); ?>                
            </td>
            <td colspan="2">                
                Cambio: <div id="montoCambio" style="float: right;">0</div>
            </td>
        </tr>         
        
        <tr>
            <td colspan="2">
                NIT :                
            </td>
            <td colspan="2">
                <?php echo $this->Form->text('nit', array('id'=>'pagado', 'placeholder'=>0, 'class'=>'span10')); ?>                
            </td>           
        </tr>
        
        <tr>
            <td colspan="2">
                NOMBRE :                
            </td>
            <td colspan="2">
                <?php echo $this->Form->text('nombre', array('id'=>'pagado', 'placeholder'=>'Ingrese Nombre', 'class'=>'span10')); ?> 
                <?php echo $this->Form->hidden('nombrealumno', array('value'=>$nombreAlumno)); ?>               
            </td>           
        </tr>
        
        <tr>
            <td colspan="4">
                <input class="btn btn-success" id="btPagar" type="submit" value="Pagar" />
            </td>
        </tr>
        
    </tbody>
</table>
</form>

  </div>
</div>
<script type="text/javascript">

//sumas el valor de los check
var $cambio = 0;
var countChecked = function() {
	var sum = 0;
	$("input[type=checkbox]:checked").each(function() {
		sum += parseInt(this.value);
		//sum += parseInt($(this).value());

		//modifica cambio
		$cambio = $("#pagado").val() - $("#montoTotal").val();
		$("#montoCambio").html($cambio);

	});
	$("#montoTotal").val(sum);
    $("#montoTotalH").val(sum);
	//$("#log").html(sum);
	//console.log(sum);                  
}

countChecked();
$("input[type=checkbox]").on("click", countChecked);


$("#pagado").focusout(function() {

	$cambio = $("#pagado").val() - $("#montoTotal").val();
	$("#montoCambio").html($cambio);
	console.log($cambio);
});
</script>
<?php else:?>
<div class="row-fluid">
<div class="span8">
   <h3>
   No exiten planes registrados para este alumno
   </h3>
   
</div>
<div class="span10">
<?php
                            echo $this->Ajax->link(
                                    $this->Html->image("iconos/cuentas.png"), 
                                    array(
                                        'controller' => 'alumnos', 
                                        'action' => 'ajaxanadeplan', $idAlumno
                                    ), 
                                    array(
                                        'update' => 'plan',
                                        'title' => 'Anade plan del Alumno',
                                        'escape' => false
                                   )
                            );
                            ?>
</div>
<div class="span6" id="plan">

</div>
</div>
<?php endif; ?>